<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="分类名称" prop="classificationName">
        <el-input v-model="dataForm.classificationName" placeholder="分类名称"></el-input>
      </el-form-item>
      <el-form-item label="分类编号" prop="classificationNo">
        <el-input v-model="dataForm.classificationNo" placeholder="分类编号"></el-input>
      </el-form-item>
      <el-form-item label="使用折扣">
        <el-radio v-model="dataForm.discountStatus" label="1">是</el-radio>
        <el-radio v-model="dataForm.discountStatus" label="2">否</el-radio>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio v-model="dataForm.status" label="1">正常</el-radio>
        <el-radio v-model="dataForm.status" label="2">停用</el-radio>
      </el-form-item>
      <!--    <el-form-item label="业务提成" prop="businessCommission">-->
      <!--      <el-input v-model="dataForm.businessCommission" placeholder="业务提成"></el-input>-->
      <!--    </el-form-item>-->
      <!--      <el-form-item v-show="dataForm.id == 0" label="使用范围" prop="useRangeList">-->
      <!--        <el-checkbox-group v-model="dataForm.useRangeList">-->
      <!--          <el-checkbox v-for="range in useRangeList" :key="range.id" :label="range.id">{{-->
      <!--              range.orderName-->
      <!--            }}-->
      <!--          </el-checkbox>-->
      <!--        </el-checkbox-group>-->
      <!--      </el-form-item>-->
      <el-form-item label="备注">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      disabledSubmit: false,
      dataForm: {
        id: 0,
        parentId: '',
        sellerId: '',
        classificationName: '',
        classificationNo: '',
        discountStatus: '1',
        status: '1',
        sort: '',
        useRange: '',
        useRangeList: [1, 2, 3],
        businessCommission: '',
        remark: '',
      },
      useRangeList: [],
      dataRule: {
        parentId: [
          {required: true, message: '上级分类ID不能为空', trigger: 'blur'}
        ],
        sellerId: [
          {required: true, message: '所属租户不能为空', trigger: 'blur'}
        ],
        classificationName: [
          {required: true, message: '分类名称不能为空', trigger: 'blur'}
        ],
        classificationNo: [
          {required: true, message: '分类编号不能为空', trigger: 'blur'}
        ],
        discountStatus: [
          {required: true, message: '是否能使用折扣(1能2不能)不能为空', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '状态(1正常2停用)不能为空', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '排序不能为空', trigger: 'blur'}
        ],
        // useRangeList: [
        //   { required: true, message: '使用范围不能为空', trigger: 'blur' }
        // ],
        businessCommission: [
          {required: true, message: '业务提成不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init(id) {
      this.$http({
        url: this.$http.adornUrl('/store/tstoreordermode/list'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        this.useRangeList = data.page.list
      })
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/store/tstorefoodtype/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.parentId = data.tStoreFoodType.parentId
              this.dataForm.sellerId = data.tStoreFoodType.sellerId
              this.dataForm.classificationName = data.tStoreFoodType.classificationName
              this.dataForm.classificationNo = data.tStoreFoodType.classificationNo
              this.dataForm.discountStatus = data.tStoreFoodType.discountStatus.toString()
              this.dataForm.status = data.tStoreFoodType.status.toString()
              this.dataForm.sort = data.tStoreFoodType.sort
              this.dataForm.useRangeList = data.tStoreFoodType.useRangeList
              this.dataForm.businessCommission = data.tStoreFoodType.businessCommission
              this.dataForm.remark = data.tStoreFoodType.remark
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.disabledSubmit = true
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/store/tstorefoodtype/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'parentId': this.dataForm.parentId,
              'sellerId': this.dataForm.sellerId,
              'classificationName': this.dataForm.classificationName,
              'classificationNo': this.dataForm.classificationNo,
              'discountStatus': this.dataForm.discountStatus,
              'status': this.dataForm.status,
              'sort': this.dataForm.sort,
              'useRangList': this.dataForm.useRangeList,
              'businessCommission': this.dataForm.businessCommission,
              'remark': this.dataForm.remark
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.disabledSubmit = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.disabledSubmit = false
              this.$message.error(data.msg)
            }
          })
        } else {
          this.disabledSubmit = false
        }
      })
    }
  }
}
</script>
